﻿using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;

namespace Sofire.AOP
{
    class ConstructionCallSink : SinkBase
    {
        public ConstructionCallSink(IMessageSink nextSink) : base(nextSink) { }

        public override IMessage SyncProcessMessage(IMessage msg)
        {
            var constructionCallMessage = msg as IConstructionCallMessage;

            if(constructionCallMessage == null) return base.SyncProcessMessage(msg);
            var action = GetAction(constructionCallMessage);

            if(!AOPSetting.EnterInvoke(null, action, constructionCallMessage)) return base.SyncProcessMessage(msg);

            var r = base.SyncProcessMessage(constructionCallMessage) as IMethodReturnMessage;

            AOPSetting.LeaveInvoke(r.ReturnValue, action, constructionCallMessage, r);
            return r;
        }
    }
}
